package com.cskaoyan.service;

import com.cskaoyan.bean.bo.wx.SaveListBO;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.AddressList;
import com.cskaoyan.bean.vo.RegionListVO;
import com.cskaoyan.bean.vo.wx.AddressListVO;
import com.cskaoyan.bean.vo.wx.mygroupon.AddressDetailVO;
import com.cskaoyan.dao.RegionMapper;
import com.cskaoyan.dao.UserAddressMapper;
import com.sun.jndi.cosnaming.IiopUrl;
import org.apache.tomcat.jni.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

@SuppressWarnings("all")
@Service
public class AddressImpl implements AddressService {
    @Autowired
    UserAddressMapper userAddressMapper;

    @Autowired
    RegionMapper regionMapper;

    @Override
    public AddressListVO addressList(int id) {
        UserAddressExample userAddressExample = new UserAddressExample();
        UserAddressExample.Criteria criteria = userAddressExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<UserAddress> userAddresses = userAddressMapper.selectByExample(userAddressExample);
        List<AddressList> lists = new ArrayList<>();
        for (UserAddress userAddress : userAddresses) {
            String province = queryAdressProvince(userAddress.getProvinceId());
            String city = queryAdressCity(userAddress.getCityId());
            String area = queryAdressArea(userAddress.getAreaId());
            lists.add(new AddressList(userAddress.getIsDefault(), province + city + area + " " + userAddress.getAddress(),
                    userAddress.getName(), userAddress.getMobile(), userAddress.getId()));
        }
        return new AddressListVO(lists);
    }

    @Override
    public AddressDetailVO addressDetail(int id) {
        UserAddressExample userAddressExample = new UserAddressExample();
        UserAddressExample.Criteria criteria = userAddressExample.createCriteria();
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(id);
        String province = queryAdressProvince(userAddress.getProvinceId());
        String city = queryAdressCity(userAddress.getCityId());
        String area = queryAdressArea(userAddress.getAreaId());
        return new AddressDetailVO(userAddress.getIsDefault(), userAddress.getAreaId(),
                userAddress.getAddress(), city, area, userAddress.getName(), userAddress.getMobile(),
                userAddress.getId(), userAddress.getCityId(), province, userAddress.getProvinceId());
    }

    @Override
    public void deleteList(int id) {
        UserAddressExample userAddressExample = new UserAddressExample();
        UserAddress userAddress = new UserAddress();
        userAddress.setId(id);
        userAddress.setDeleted(true);
        userAddressMapper.updateByPrimaryKeySelective(userAddress);
    }

    @Override
    public List<RegionListVO> regionList(int pid) {
        List<RegionListVO> regionListVOS = new ArrayList<>();
        RegionExample example = new RegionExample();
        RegionExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(pid);
        List<Region> regions = regionMapper.selectByExample(example);
        for (Region region : regions) {
            regionListVOS.add(new RegionListVO(region.getId(), region.getPid(), region.getName(),
                    region.getType(), region.getCode()));
        }
        return regionListVOS;
    }

    @Override
    public void SaveAddress(SaveListBO saveListBO, User user) {
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        UserAddressExample userAddressExample = new UserAddressExample();
        Date addDate;
        if (saveListBO.getId() != 0) {
            addDate = userAddressMapper.selectByPrimaryKey(saveListBO.getId()).getAddTime();
        } else {
            addDate = new Date();
        }
        UserAddress userAddress = new UserAddress(saveListBO.getId() == 0 ? null : saveListBO.getId(), saveListBO.getName(), user.getId(),
                saveListBO.getProvinceId(), saveListBO.getCityId(), saveListBO.getAreaId(), saveListBO.getAddress(),
                saveListBO.getMobile(), saveListBO.isIsDefault(), addDate, new Date(), false);
        if (userAddress.getIsDefault()) {
            UserAddressExample userAddressExampleByDefault = new UserAddressExample();
            UserAddressExample.Criteria criteria = userAddressExampleByDefault.createCriteria();
            if (saveListBO.getId() != 0) {
                criteria.andIdNotEqualTo(userAddress.getId());
            }
            criteria.andUserIdEqualTo(user.getId());
            UserAddress userAddressByDault = new UserAddress();
            userAddressByDault.setIsDefault(false);
            userAddressMapper.updateByExampleSelective(userAddressByDault, userAddressExampleByDefault);
        }
        if (saveListBO.getId() != 0) {
            userAddressMapper.updateByPrimaryKeySelective(userAddress);
        } else {
            userAddressMapper.insertSelective(userAddress);
        }
    }

    private String queryAdressArea(Integer provinceId) {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        Region province = regionMapper.selectByPrimaryKey(provinceId);
        return province.getName();
    }

    private String queryAdressCity(Integer cityId) {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        Region province = regionMapper.selectByPrimaryKey(cityId);
        return province.getName();
    }

    private String queryAdressProvince(Integer areaId) {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        Region province = regionMapper.selectByPrimaryKey(areaId);
        return province.getName();
    }
}
